package logic

import (
	"context"
	"go-zero-example/cmd/userrpc/internal/dao"
	"go-zero-example/cmd/userrpc/internal/model"
	"go-zero-example/cmd/userrpc/internal/util"
	"strconv"

	"go-zero-example/cmd/userrpc/internal/svc"
	"go-zero-example/cmd/userrpc/pb/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RePhoneLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRePhoneLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RePhoneLogic {
	return &RePhoneLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// RePhone 修改手机号
func (l *RePhoneLogic) RePhone(in *userrpc.RePhoneRequest) (*userrpc.RePhoneResponse, error) {
	res := new(userrpc.RePhoneResponse)
	user := new(model.User)
	user.Id = util.ConvertToInternalID(strconv.FormatInt(in.UserId, 10))
	user.Phone = in.Phone
	userDao := new(dao.UserDao)
	err := userDao.RePhoneByUid(user)
	if err != nil {
		res.Message = "修改失败"
		return res, err
	}
	return res, nil
}
